<?php
/**
 * composer require workerman/gateway-worker
 *
 * run with command
 * php workerman.php restart
 *
 * 后台运行
 * php workerman.php restart 1>/home/www/wwwroot/dmjz/storage/logs/workerman_output.log 2>&1 &
 *
 * 客户端连接测试
 * telnet 127.0.0.1 9100
 *
 * @author lxp 20170126
 */

ini_set('display_errors', 'on');

use Workerman\Worker;
use GatewayWorker\Register;
use GatewayWorker\Gateway;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Lib\Gateway AS GatewayLib;
use Workerman\Lib\Timer;
use App\Dao\LogDao;
use App\Models\Messages;
use App\Models\Users;
use App\Models\Shops;


// 初始化Laravel框架，保证路由'/'中没有代码中断（die，exit等）
require __DIR__ . '/vendor/autoload.php';
$app = require_once __DIR__ . '/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle($request = Illuminate\Http\Request::capture());

/**
 * 相关业务逻辑处理
 *
 * @author lxp
 */
class Events
{
    /**
     * 当客户端连接时触发
     *
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        $arr['client_id']=$client_id;
        $arr['type']='bind';
        $arr['send_type']='';
        $arr['send_content']='';
        GatewayLib::sendToClient($client_id, json_encode($arr));
    }

    /**
     * 当客户端发来消息时触发
     *
     * @param int $client_id 连接id
     * @param mixed $message 具体消息
     */
    public static function onMessage($client_id, $message)
    {
        if ($message == 'exit') {
            GatewayLib::closeClient($client_id);
        }
        $message = trim($message);
        $arr=json_decode($message,true);
        LogDao::save_log('message_log',$arr);
        $message = new Messages();
        $message->content = $arr['content'];
        $user= Users::where('api_token',$arr['api_token'])->first();
        $message->uid = $user->uid;
        $message->fid =$arr['fid'];
        $message->type = $arr['type'];
        $message->uread_type=1;
        $arr['client_id']=$client_id;
        if(GatewayLib::isUidOnline($arr['fid'])){
            $message->fread_type=1;
        }else{
            $message->fread_type=0;
        }
        $message->save();
        $message->uidType=0;
        if(GatewayLib::isUidOnline($arr['fid'])){
            $message->uidType=1;
        }
        GatewayLib::sendToClient($client_id, json_encode($message));
        GatewayLib::sendToUid($arr['fid'], json_encode($message) );
        //绑定用户
        echo '[' . date('Y-m-d H:i:s') . '] [' . $client_id . '] ' . $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER['REMOTE_PORT'] . " $message\n";
    }

    /**
     * 当用户断开连接时触发
     *
     * @param int $client_id 连接id
     */
    public static function onClose($client_id)
    {
        $uid=cache($client_id);
        if(!empty($uid)){
            /*$logObj = app('logext');
            $logObj->init('api_do_log_uid'.$uid);
            $logObj->log('result: ' . json_encode(['do'=>'client_close']));
            $logObj->end();*/
            Cache::forget($client_id);
        }
        else{
            /*$logObj = app('logext');
            $logObj->init('api_do_log_client');
            $logObj->log('result: ' . json_encode(['do'=>'client_close','client_id'=>$client_id]));
            $logObj->end();*/
        }
    }

    /**
     * onWorkerStop
     *
     * @author lxp
     * @param $businessWorker
     */
    public static function onWorkerStop($businessWorker)
    {
    }
}

$registerIP = env('WM_REGISTER_IP', '172.31.48.182');
$registerPort = env('WM_REGISTER_PORT', '1238');
$gatewayPort = env('WM_GATEWAY_PORT', '9100');

// register 服务必须是text协议
$register = new Register("text://0.0.0.0:{$registerPort}");

// gateway 进程，这里使用Text协议，可以用telnet测试
$gateway = new Gateway("websocket://0.0.0.0:{$gatewayPort}");
// gateway名称，status方便查看
$gateway->name = 'DM_Gateway';
// gateway进程数
$gateway->count = 4;
// gateway起始端口
$gateway->startPort = env('WM_GATEWAY_STARTPORT', 3000);
// 服务注册地址
$gateway->registerAddress = "$registerIP:$registerPort";
// 心跳间隔
//$gateway->pingInterval = 10;
// 心跳数据
//$gateway->pingData = '{"type":"ping"}';

// bussinessWorker 进程
$worker = new BusinessWorker();
// worker名称
$worker->name = 'DM_BusinessWorker';
// bussinessWorker进程数量
$worker->count = 4;
// 服务注册地址
$worker->registerAddress = "$registerIP:$registerPort";

// 日志存储路径
$path_dir = storage_path() . '/logs/' . date('Y-m', time()) . '/';
m_mkdir($path_dir);
$path_dir .= date('d', time()) . '/';
m_mkdir($path_dir);

// Workerman运行日志
Worker::$logFile = $path_dir . 'workerman_runtime.log';
// 运行所有服务
Worker::runAll();
